package com.itfreer.power.dao.user;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ConstraintMode;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ForeignKey;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.beans.BeanUtils;

import com.itfreer.form.api.JdbcBaseEntity;
import com.itfreer.power.entity.user.UserRoleEntity;

/**
 * 定义用户角色实体
 */
@Entity(name = "sso_user_role")
@Table(indexes = { 
        @Index(name="s_user_role_user_id", columnList="user_id"),
        @Index(name="s_user_role_role_id", columnList="role_id") })
public class JdbcUserRoleEntity implements JdbcBaseEntity<UserRoleEntity>, Serializable {
	
	private static final long serialVersionUID = -4505057087603187400L;

	/**
	 * 表ID值
	 */
	@Id
	@Column(name = "id", nullable = false)
	private String id;
	
	/**
	 * 用户主键
	 */
	@Column(name = "user_id", length = 50, nullable = true)
	private String userId;

	/**
	 * 角色主键
	 */
	@Column(name = "role_id", length = 50, nullable = true)
	private String roleId;
	
	@ManyToOne(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY)
	@JoinColumn(name = "id", insertable=false, updatable=false,
		foreignKey = @ForeignKey(name = "none",value = ConstraintMode.NO_CONSTRAINT))
	private JdbcUserInfoEntity user;

	/**
	 * 表ID值
	 */
	public String getId() {
		return id;
	}

	/**
	 * 表ID值
	 */
	public void setId(String id) {
		this.id = id;
	}
	
	/**
	 * 用户主键
	 */
	public String getUserId() {
		return userId;
	}

	/**
	 * 用户主键
	 */
	public void setUserId(String userId) {
		this.userId = userId;
	}

	/**
	 * 角色主键
	 */
	public String getRoleId() {
		return roleId;
	}

	/**
	 * 角色主键
	 */
	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}
	
	@Override
	public void from(UserRoleEntity t) {
		BeanUtils.copyProperties(t, this);
	}

	@Override
	public UserRoleEntity toEntity() {
		UserRoleEntity toEntity = new UserRoleEntity();
		BeanUtils.copyProperties(this, toEntity);
		return toEntity;
	}
}
